import java.sql.*
import org.datacoder.*
import org.datacoder.benchmark.*
import org.datacoder.geocoding.*
import org.datacoder.geocoding.datastructures.intervals.*
import org.datacoder.geocoding.datastructures.points.*
import org.datacoder.geocoding.datastructures.rectangles.*
import org.datacoder.geocoding.intervaltree.*
import org.datacoder.geocoding.jurisdiction.*
import org.datacoder.util.*
import org.springframework.orm.hibernate3.SessionFactoryUtils

import com.vividsolutions.jts.operation.valid.*
import com.vividsolutions.jts.geom.*
import com.vividsolutions.jts.geom.impl.*

import grails.util.GrailsUtil

/*
 * Runs on application startup.  The init closure at the end
 * is called initially; it calls other methods in the class,
 * depending on the values of the constants at the top.
 */
public class BootStrap {

    private static final boolean DEV_PERFORM_OTHER_CODE = false
    private static final boolean DEV_CREATE_DATA = false
    private static final boolean DEV_TEST_DATA = false
    private static final List<String> DEV_CREATE_DATA_SET = ["MT","ND","SD","MN","NE","IA","MO","WI","IL","MI","KY","AR","WA","OR","ID","WY","OH","PA","NY"]
    AllService allService

    private void devBootstrap(){
        try{
            if(DEV_PERFORM_OTHER_CODE){
                // No code at the moment.
                println "Done."
                System.exit(0)
            }else if(DEV_CREATE_DATA){
                SampleData sampleData = new SampleData(allService)
                sampleData.createSampleData(DEV_CREATE_DATA_SET)
                println "Done."
                System.exit(0)
            }else if(DEV_TEST_DATA){
                TestSuite.run()
                println "Done."
                System.exit(0)
            }else{
                initializeGeocodingService()
            }
        }catch(Exception e){
            println ""
            e.printStackTrace(System.out)
            System.exit(1)
        }
    }
    private void testBootstrap(){
        initializeGeocodingService()
    }
    private void initializeGeocodingService(){
        allService.geocodingService.init(
            Jurisdiction.findAll(),
            Buckets,
            Buckets,
            Cells,
            PairedIntervalTrees
        )
    }
    private boolean doImportantStuff(){
        javax.sql.DataSource dataSource = SessionFactoryUtils.getDataSource(allService.sessionFactory)
        Connection con = dataSource.getConnection()
        con.createStatement().execute("SET WRITE_DELAY 0")
        for(dc in allService.grailsApplication.getArtefacts("Domain")){
            dc.getClazz().count()
        }
        allService.jurisdictionService.init(allService)
        allService.stateService.init(allService)
        String environment = GrailsUtil.getEnvironment()
        boolean isDev = true
        if(environment.equals(allService.grailsApplication.ENV_TEST)){
            isDev = false
        }
        return isDev
    }

    def init = {servletContext ->
        boolean isDev = doImportantStuff()
        println Jurisdiction.findAll().size()
        if(isDev){
            devBootstrap()
        }else{
            testBootstrap()
        }
    }
    def destroy = {}

}